<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 0:19
 * QQ:1467572213
 */

namespace app\dg\model;

use think\Model;
use think\Db;
use think\facade\Config;

class Login extends Model
{
    public static function login($info)
    {
        $where = [
            ['username', 'like', $info['account']],
            ['phone', 'like', $info['account']],
        ];
        $check_info = Db::name('fd_user')->whereOr($where)->find();
        if ($check_info['status'] == 2) return 'error_status';
        if ($check_info['status'] == 1){
            if ($check_info['password'] == md5($info['password'])){
                Db::name('fd_user')->where('id',$check_info['id'])->setField('last_ip',get_ip());
                Db::name('fd_user')->where('id',$check_info['id'])->setField('last_time',time());
                return $check_info;
            }else{
                return 'error';
            }
        }else{
            return 'error';
        }
    }

    public static function reg($info,$code)
    {

        $reg = Db::name('fd_user')->insert($info);
        if ($reg == true){
            return 'success';
        }else{
            return 'error';
        }
        die;

        $where['code'] = $code;
        $where['type'] = 1;
        $where['status'] = 0;
        $check_code = Db::name('fd_sms_code')->where($where)->find();
        if ($check_code){
            $reg = Db::name('fd_user')->insert($info);
            if ($reg == true){
                Db::name('fd_sms_code')->where($where)->setField('status',1);
                return 'success';
            }else{
                return 'error';
            }
        }else{
            return 'error';
        }
    }

    public static function forget($info)
    {
        $where['phone'] = $info['phone'];
        $where['code'] = $info['code'];
        $where['type'] = 4;
        $where['status'] = 0;
        $check_code = Db::name('fd_sms_code')->where($where)->find();
        if ($check_code){
            $edit_password = Db::name('fd_user')
                ->where('phone',$check_code['phone'])
                ->setField('password',md5($info['password']));
            if ($edit_password == true){
                Db::name('fd_sms_code')->where($where)->setField('status',1);
                return 'success';
            }else{
                return 'error';
            }
        }else{
            return 'error_code';
        }

    }

    public static function check_username($username)
    {
        $check = Db::name('fd_user')->where('username',$username)->count();
        if ($check == 0){
            return 'success';
        }else{
            return 'error';
        }
    }

    public static function check_phone($phone)
    {
        $check = Db::name('fd_user')->where('phone',$phone)->count();
        if ($check == 0){
            return 'success';
        }else{
            return 'error';
        }
    }

    public static function sms_code($phone,$type)
    {
        $account = Config::get('sms_code_account');
        $key = Config::get('sms_code_key');
        $zone = Config::get('sms_code_zone');
        $datetime = date('YmdHis');
        $sign = md5($account.$key.$datetime);
        $code = \app\dg\model\StringCode::randString(4,1);
        $url = "http://sms.skylinelabs.cc:20003/sendsmsV2?account=$account&sign=$sign&datetime=$datetime";
        if ($type == 4){
            $data = [
                'content' => '[EARNHUB] your Mobile Registration Code is '.$code.' Valid for 5 Minutes',
                'numbers' => $zone.$phone,
            ];
        }else{
            $data = [
                'content' => 'Your SMS verification code is:'.$code,
                'numbers' => $zone.$phone,
            ];
        }

        $send_code = self::send_post($url, $data);
        $send_code['status'] = 0;
        if ($send_code['status'] == 0){
            $sms_code['phone'] = $phone;
            $sms_code['code'] = $code;
            $sms_code['type'] = $type;
            $sms_code['status'] = 0;
            $sms_code['create_time'] = time();
            $add_sms_code = Db::name('fd_sms_code')->insert($sms_code);
            if ($add_sms_code == true){
                return ['code' => 'success'];
            }else{
                return ['code' => 'error'];
            }
        }else{
            return ['code' => 'error'];
        }
    }

    public static function check_code($code)
    {
        $where['code'] = $code;
        $where['type'] = 1;
        $where['status'] = 0;
        $check = Db::name('fd_sms_code')->where($where)->find();
        if ($check['status'] == 0){
            return $check['phone'];
        }else{
            return 'error';
        }
    }

    public static function get_pid_info($invite_code)
    {
        $pid_info = Db::name('fd_user')->where('invite_code',$invite_code)->value('id');
        if ($pid_info){
            return $pid_info;
        }else{
            return 0;
        }
    }

    public static function get_invite_code()
    {
        $invite_code = \app\dg\model\StringCode::randString(6,1);
        $check_invite_code = Db::name('fd_user')->where('invite_code',$invite_code)->count();
        if ($check_invite_code > 0){
            self::get_invite_code();
        }else{
            return $invite_code;
        }
    }

    public static function send_post($url, $data)
    {
        header("Content-type:application/json;charset=utf-8");
        $data = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type：application/json;charset=UTF-8",
                "Content-Length: " . strlen($data))
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return = json_decode($return_content, true);
        return $return;
    }

}